package com.gxitsky.common.rsa;

import com.gxitsky.common.utils.RSAUtil;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

/**
 * @author gxing
 * @desc TODO
 * @date 2021/12/28
 */
public class TextRSATest {


    public static void main(String[] args) throws Exception {
        String content = "Hello World!";
        RSAUtil.RSAKeyPair keyPair = RSAUtil.genKeyPair();

//        String sign = RSAUtil.sign(content, keyPair.getPrivateKey());
//        boolean b = RSAUtil.verifySign(content, sign, keyPair.getPublicKey());
//        System.out.println(b);


        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(1024);
        KeyPair pair = generator.generateKeyPair();
        RSAPrivateKey privateKey = (RSAPrivateKey) pair.getPrivate();
        RSAPublicKey publicKey = (RSAPublicKey) pair.getPublic();
        System.out.println("publicKey exponent:" + publicKey.getPublicExponent());
        System.out.println("publicKey modules:" + publicKey.getModulus());
        System.out.println("publicKey format:" + publicKey.getFormat());
        System.out.println("---------------------华丽的分割线-------------------------");
        System.out.println("privateKey exponent:" + privateKey.getPrivateExponent());
        System.out.println("privateKey modules:" + privateKey.getModulus());
        System.out.println("privateKey format:" + privateKey.getFormat());


//        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
//        byte[] encrypt1 = RSAUtil.encrypt(bytes, keyPair.getPublicKey());
//        byte[] decrypt = RSAUtil.decrypt(encrypt1, keyPair.getPrivateKey());
//        String result = new String(decrypt);
//        System.out.println(result);

//        String encrypt = RSAUtil.encryptStr(content, keyPair.getPublicKey());
//        System.out.println("加密后:" + encrypt);

//        String decrypt = RSAUtil.decryptStr(encrypt, keyPair.getPrivateKey());
//        System.out.println("解密后：" + decrypt);

//        String encrypt = RSAUtil.encryptStr(content, keyPair.getPublicKey());
//        System.out.println("加密后:" + encrypt);
//
//        String decrypt = RSAUtil.decryptStr(encrypt, keyPair.getPrivateKey());
//        System.out.println("解密后：" + decrypt);

//        String encrypt = RSAUtil.encryptPriKeyStr(content, keyPair.getPrivateKey());
//        System.out.println("加密后:" + encrypt);
//
//        String decrypt = RSAUtil.decryptPubKeyStr(encrypt, keyPair.getPublicKey());
//        System.out.println("解密后：" + decrypt);
    }
}
